from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from django.views import View
from rest_framework.response import Response
from rest_framework.views import APIView
from myapp import models
# Create your views here.
from functools import wraps
from django.utils.decorators import method_decorator
from .serve import *


class Users(APIView):
    def get(self,request):
        use = User.objects.all()
        user_ser = UserSer(use,many=True)

        return Response({
            'data':user_ser.data
        })


class AddBook(APIView):
    def get(self, request):
        title = request.GET.get('title')
        price = request.GET.get('price')
        uid = request.GET.get('id')
        print(title,price,uid)

        users = User.objects.get(id=int(uid))
        print(users)
        Book.objects.create(title=title,price=price,users=users)

        return Response({
            'data': '添加成功'
        })

#
# 获取图书信息
class Books(APIView):
    def get(self, request):
        book = Book.objects.all()
        book_ser = BookSer(book, many=True)

        return Response({
            'data': book_ser.data
        })
#
#
# # 删除图书
class DelBook(APIView):
    def get(self, request):
        id = request.GET.get('id')
        book = Book.objects.get(id=int(id))
        book.delete()

        return Response({
            'data': '删除成功'
        })


